package com.pet.serve.controller;

import com.pet.serve.entity.Message;
import com.pet.serve.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.data.domain.Page;

import java.util.List;

@RestController
@RequestMapping("/api/message")
public class MessageController {

    private final MessageService messageService;

    @Autowired
    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    // 分页获取所有留言
    @GetMapping
    public ResponseEntity<Page<Message>> getMessages(@RequestParam int pageNo, @RequestParam int pageSize) {
        Page<Message> messages = messageService.getMessages();
        return ResponseEntity.ok(messages);
    }

    // 删除单条留言
    @DeleteMapping("/{messageId}")
    public ResponseEntity<Void> deleteMessage(@PathVariable Long messageId) {
        messageService.deleteMessage(messageId);
        return ResponseEntity.noContent().build();
    }

    // 批量删除留言
    @DeleteMapping
    public ResponseEntity<Void> deleteMessages(@RequestBody List<Long> messageIds) {
        messageService.deleteMessages(messageIds);
        return ResponseEntity.noContent().build();
    }

    // 获取所有留言用于导出
    @GetMapping("/all")
    public ResponseEntity<List<Message>> getAllMessages() {
        List<Message> messages = messageService.getAllMessages();
        return ResponseEntity.ok(messages);
    }

    // 导出当前页的留言
    @PostMapping("/export/current")
    public ResponseEntity<Page<Message>> exportCurrentPageMessages(@RequestParam int pageNo, @RequestParam int pageSize) {
        Page<Message> messages = messageService.exportCurrentPageMessages();
        return ResponseEntity.ok(messages);
    }
}
